package com.example.car

import android.os.Bundle
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.example.car.adapter.BrandListAdapter
import com.example.car.bean.BrandListBean
import com.example.car.bean.LogoBean
import com.example.car.databinding.ActivityBrandListBinding
import com.example.car.utils.CarApi
import com.example.car.utils.RetrofitUtils
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class BrandListActivity : AppCompatActivity() {
    val binding: ActivityBrandListBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_brand_list)
    }
    val mDatas = mutableListOf<BrandListBean.ResultBean>()
    val adapter: BrandListAdapter by lazy {
        BrandListAdapter(this, mDatas)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val window: Window = this@BrandListActivity.window
        window.statusBarColor = ContextCompat.getColor(this@BrandListActivity, R.color.black)

        val bean: LogoBean.ResultBean = intent.getSerializableExtra("bean") as LogoBean.ResultBean
        binding.title = bean.brand_name
        binding.adapter = adapter
        loadDatas(bean.id)
    }

    private fun loadDatas(id: String) {
        val api = RetrofitUtils.getApi(CarApi.base_url, CarApi::class.java)
        val carInfo = api.getBrandList(CarApi.key, id)

        carInfo?.enqueue(object : Callback<BrandListBean> {
            override fun onResponse(call: Call<BrandListBean>, response: Response<BrandListBean>) {
                val bean: BrandListBean? = response.body()
                adapter.addDatas(bean?.getResult())
            }

            override fun onFailure(call: Call<BrandListBean>, t: Throwable) {
            }

        })

    }
}